#pragma once

#include<iostream>
#include<vector>
#include<cstdio>
#include <list>
#include <assert.h>
#include <cstring>
using namespace std;
extern int huangan;

namespace linhui
{
	template<class T>
	class vector
	{
	public:
		//typedef T* iterator;
		using iterator = T*;
		using const_iterator = const T*;
		//vector() = default;
		vector()
			:_start(nullptr),_finish(nullptr),_end_of_storage(nullptr)
		{

		}
		vector(size_t num , const T& val)
			:_start(new T[num])
		{
			for (size_t i = 0; i < num; i++)
			{
				*(_start + i) = val;
			}
			_finish = _end_of_storage = _start + num;
		}
		vector(int num, const T& val)
			:_start(new T[num])
		{
			for (int i = 0; i < num; i++)
			{
				*(_start + i) = val;
			}
			_finish = _end_of_storage = _start + num;
		}
		template<class inputIterator>
		vector(inputIterator begin, inputIterator end)
		{
			while (begin != end)
			{
				push_back(*begin);
				begin++;
			}
		}
		/*vector(iterator begin, iterator end)
		{
			while (begin != end)
			{
				push_back(*begin);
				begin++;
			}
		}*/
		vector(vector<T>& obj)
		{
			/*vector<T> tmp(obj.cbegin(),obj.cend());
			swap(tmp);*/
			reserve(obj.capacity());
			for (auto& au : obj)
			{
				push_back(au);
			}
		}
		vector<T>& operator=(vector<T> obj)
		{
			if((*this)._start != obj._start)
			swap(obj);
			return *this;
		}
		~vector()
		{
			delete[] _start;
			_finish = _end_of_storage = nullptr;
		}
		T& operator[](size_t index)
		{
			return *(_start + index);
		}
		T operator[](size_t index) const
		{
			
			return *(_start + index);
		}
		void swap(vector<T>& obj) //�˴������û����޵ݹ鵼�¿�������
		{
			std::swap(_start, obj._start);
			std::swap(_finish, obj._finish);
			std::swap(_end_of_storage, obj._end_of_storage);
		}
		size_t size() const
		{
			return _finish - _start;
		}
		size_t capacity() const
		{
			return _end_of_storage - _start;
		}
		iterator begin() 
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator cbegin()const
		{
			return _start;
		}
		const_iterator cend()const
		{
			return _finish;
		}
		void reserve(size_t newCapacity)
		{
			if (newCapacity > capacity())
			{
				size_t oldSize = size();
				iterator newStart = new T[newCapacity];
				//memcpy(newStart, _start, sizeof(T) * size());
				for(int i = 0;i<oldSize;i++)
				{
					*(newStart+i) = *(_start+i); 
				}
				_start = newStart;
				_finish = _start + oldSize;
				_end_of_storage = _start + newCapacity;
			}
		}
		void resize(size_t newsize, const T& val = T())
		{
			if (newsize < size())
			{
				_finish = _start + newsize;
			}
			else 
			{
				reserve(newsize); //���븴��1
				size_t num = newsize - size();
				while (num--)
				{
					push_back(val);
				}
			}
		}
		void push_back(const T& val )
		{
			if (size() == capacity())
			{
				reserve(capacity() == 0 ? 4 : 2 * capacity());
			}
			_start[size()] = val;
			_finish++;
		}
		void pop_back()
		{
			if (size() > 0)
			{
				_finish--;
			}
		}
		void clear()
		{
			_finish = _start;
		}
		iterator erase(iterator pos)
		{

			iterator temPos = pos;
			if (temPos >= _start && temPos < _finish)
			{
				while (temPos != _finish - 1)
				{
					*temPos = *(temPos + 1);
					temPos++;
				}
				_finish--;
				return pos;
			}
			else
			{
				return _finish;
			}
		}
		
		iterator insert(iterator pos, const T& val)
		{
			if (size() == capacity())
			{
				size_t offSet = pos - _start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = _start + offSet;
			}
			iterator end = _finish;
			while (pos < end)
			{
				*end = *(end - 1);
				end--;
			}
			*pos = val;
			_finish++;
			return pos;
		}
		
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};
	//template<class T>
	//void print_vector(vector<T>& v)
	//{
	//	typename vector<double>::iterator it = v.begin();
	//	//auto it = v.begin();
	//	while (it != v.end())
	//	{
	//		cout << *(it++) << " ";
	//	}
	//	cout << endl;
	//	/*for (auto au : v)
	//	{
	//		cout << au << " ";
	//	}*/
	//}
	template<class container>
	void print_container(container& v)
	{
		typename container::iterator it = v.begin();
		//auto it = v.begin();
		while (it != v.end())
		{
			cout << *(it++) << " ";
		}
		cout << endl;
		/*for (auto au : v)
		{
			cout << au << " ";
		}*/
	}
}